package com.usernet.product.action.web;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.usernet.product.comm.BaseAction;
import com.usernet.product.entity.Photo;
import com.usernet.product.entity.Resources;
import com.usernet.product.entity.Search;
import com.usernet.product.entity.Software;
import com.usernet.product.entity.Type;
import com.usernet.product.tools.BaseCache;
import com.usernet.product.tools.Page;
import com.usernet.product.utils.ProductConfig;
import com.usernet.product.utils.ProductUtils;

public class IndexAction extends BaseAction {
	private static BaseCache otherCache = new BaseCache("index_other",
			ProductConfig.cacheTime);// 缓存时间

	// 首页
	@SuppressWarnings("unchecked")
	public ActionForward index(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List<Software> topsList = (List<Software>) otherCache.get("index_top_list");
		if (topsList == null) {
			topsList = photoDao.getTops(10, 1);
			otherCache.put("index_top_list", topsList);
		}

		List<Software> gameList = (List<Software>) otherCache.get("index_game_list");
		if (gameList == null) {
			gameList = photoDao.getGames(20, 1);
			otherCache.put("index_game_list", gameList);
		}

		List<Software> softList = (List<Software>) otherCache.get("index_soft_list");
		if (softList == null) {
			softList = photoDao.getSofts(20, 1);
			otherCache.put("index_soft_list", softList);
		}

		List<Resources> resList = (List<Resources>) otherCache
				.get("index_res_ist");
		if (resList == null) {
			resList = resourcesDao.getAllTypeForIndex(10, 1);
			otherCache.put("index_res_ist", resList);
		}

		request.setAttribute("topsList", topsList);
		request.setAttribute("gameList", gameList);
		request.setAttribute("softList", softList);
		request.setAttribute("resList", resList);

		request.setAttribute("sort", 1); // 默认time排序

		request.setAttribute("title", "首页");
		request.setAttribute("nav", "index");
		return mapping.findForward("index");
	}

	// 安卓软件首页
	@SuppressWarnings("unchecked")
	public ActionForward soft(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		Integer countrecord = softwareDao
				.getCountCommendByPageForWeb(ProductConfig.FLAG_SOFT);

		int pageNo = 1;
		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		Page page = new Page(countrecord, pageNo);
		List<Software> softList = softwareDao.getAllCommendByPageForWeb(
				ProductConfig.PAGE_SIZE_FOR_LIST, pageNo,
				ProductConfig.FLAG_SOFT);

		List<Software> commendList = (List<Software>) otherCache
				.get("soft_commend_list");

		if (commendList == null) {
			commendList = softwareDao.getAllTopsByPage(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1,
					ProductConfig.FLAG_SOFT); // 编辑推荐，显示10个
			otherCache.put("soft_commend_list", commendList);
		}

		List<Type> list = (List<Type>) otherCache.get("soft_type_list");
		if (list == null) {
			list = typeDao.getAllTypeByPage(1000, pageNo,
					ProductConfig.FLAG_SOFT); // 获取应用分类
			otherCache.put("soft_type_list", list);
		}

		request.setAttribute("title", "安卓应用");
		request.setAttribute("name", "应用分类");
		request.setAttribute("location", "安卓应用");
		request.setAttribute("nav", "soft");
		request.setAttribute("flag", ProductConfig.FLAG_SOFT); // 应用OR游戏OR专题
		request.setAttribute("id", 0);// 分类OR专题ID
		request.setAttribute("commendList", commendList);
		request.setAttribute("list", list);
		request.setAttribute("softList", softList);
		request.setAttribute("sort", 3); // 默认time排序
		request.setAttribute("page", page);
		return mapping.findForward("list");
	}

	// 安卓游戏首页
	@SuppressWarnings("unchecked")
	public ActionForward game(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Integer countrecord = softwareDao
				.getCountCommendByPageForWeb(ProductConfig.FLAG_GAME);

		int pageNo = 1;
		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		Page page = new Page(countrecord, pageNo);
		List<Software> softList = softwareDao.getAllCommendByPageForWeb(
				ProductConfig.PAGE_SIZE_FOR_LIST, pageNo,
				ProductConfig.FLAG_GAME);

		List<Software> commendList = (List<Software>) otherCache
				.get("game_commend_list");
		if (commendList == null) {
			commendList = softwareDao.getAllTopsByPage(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1,
					ProductConfig.FLAG_GAME); // 编辑推荐，显示10个
			otherCache.put("game_commend_list", commendList);
		}

		List<Type> list = (List<Type>) otherCache.get("game_type_list");
		if (list == null) {
			list = typeDao.getAllTypeByPage(1000, 1, ProductConfig.FLAG_GAME); // 获取应用分类
			otherCache.put("game_type_list", list);
		}

		request.setAttribute("title", "安卓游戏");
		request.setAttribute("name", "游戏分类");
		request.setAttribute("location", "安卓游戏");
		request.setAttribute("nav", "game");
		request.setAttribute("flag", ProductConfig.FLAG_GAME); // 应用OR游戏OR专题
		request.setAttribute("id", 0);// 分类OR专题ID
		request.setAttribute("commendList", commendList);
		request.setAttribute("list", list);
		request.setAttribute("softList", softList);
		request.setAttribute("sort", 3); // 默认下载次数排序
		request.setAttribute("page", page);
		return mapping.findForward("list");
	}

	// 安卓专题首页
	@SuppressWarnings("unchecked")
	public ActionForward res(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		Integer countrecord = softwareDao.getCountTopsByPage();

		int pageNo = 1;
		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		Page page = new Page(countrecord, pageNo);

		List<Software> softList = softwareDao.getAllResByPageForWeb(
				ProductConfig.PAGE_SIZE_FOR_LIST, pageNo);

		List<Software> commendList = (List<Software>) otherCache
				.get("res_commend_list");
		if (commendList == null) {
			commendList = softwareDao.getAllResByPage(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1); // 编辑推荐，显示10个
			otherCache.put("res_commend_list", commendList);
		}

		List<Resources> list = (List<Resources>) otherCache.get("res_type_list");
		if (list == null) {
			list = resourcesDao.getAllTypeByPage(1000, pageNo); // 获取专题分类
			otherCache.put("res_type_list", list);
		}

		request.setAttribute("title", "安卓专题");
		request.setAttribute("name", "专题分类");
		request.setAttribute("location", "安卓专题");
		request.setAttribute("nav", "res");
		request.setAttribute("flag", ProductConfig.FLAG_RES); // 应用OR游戏OR专题
		request.setAttribute("id", 0);// 分类OR专题ID
		request.setAttribute("softList", softList);
		request.setAttribute("commendList", commendList);
		request.setAttribute("list", list);
		request.setAttribute("sort", 3); // 默认time排序
		request.setAttribute("page", page);
		return mapping.findForward("list");
	}

	// 分类详细信息
	@SuppressWarnings("unchecked")
	public ActionForward gameList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		List<Software> softList = null;

		int pageNo = 1;

		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		int sort = Integer.parseInt(request.getParameter("s"));

		int id = Integer.parseInt(request.getParameter("i"));
		int flag = Integer.parseInt(request.getParameter("flag"));
		String nav = request.getParameter("nav");

		List<Software> commendList = softwareDao.getAllTopsByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, flag); // 编辑推荐，显示10个

		String title = null;
		String location = null;
		String name = null;
		// 分类
		List<Type> list = (List<Type>) otherCache.get("game_list_type_list");
		if (list == null) {
			list = typeDao.getAllTypeByPage(1000, 1, flag); // 获取应用分类
			otherCache.put("game_list_type_list", list);
		}

		if (id == 0) {
			// 所有信息
			name = "游戏分类";
			title = "安卓游戏";
			location = "安卓游戏";
			softList = softwareDao.getAllSortByPageForWeb(
					ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort,
					ProductConfig.FLAG_GAME);
			Integer countrecord = softwareDao.getAllSortByPageForWeb(sort,
					ProductConfig.FLAG_GAME);

			Page page = new Page(countrecord, pageNo);
			request.setAttribute("page", page);

		} else {
			// 分类
			Type type = (Type) typeDao.getById(Type.class, id);

			if (type != null) {
				title = type.getName();
				location = type.getName();
				name = "游戏分类";
				softList = softwareDao.getAllTypeInfoByPageAndSort(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, flag, id,
						sort);
				Integer countrecord = softwareDao
						.getCountTypeInfoByPageAndSort(flag, id, sort);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			} else {
				name = "游戏分类";
				title = "安卓游戏";
				location = "安卓游戏";
				softList = softwareDao.getAllSortByPageForWeb(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort,
						ProductConfig.FLAG_GAME);
				Integer countrecord = softwareDao.getAllSortByPageForWeb(sort,
						ProductConfig.FLAG_GAME);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			}
		}
		request.setAttribute("list", list); // 分类OR专题列表
		request.setAttribute("title", title);// 网页标题
		request.setAttribute("location", location);// 当前位置
		request.setAttribute("nav", nav); // 导航栏
		request.setAttribute("name", name); // 分类/专题名称
		request.setAttribute("flag", flag); // 应用OR游戏OR专题
		request.setAttribute("id", id);// 分类OR专题ID
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("softList", softList); // 软件列表
		request.setAttribute("sort", sort); // 排序方式

		return mapping.findForward("list");
	}

	@SuppressWarnings("unchecked")
	public ActionForward softList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		List<Software> softList = null;

		int pageNo = 1;

		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		int sort = Integer.parseInt(request.getParameter("s"));

		int id = Integer.parseInt(request.getParameter("i"));
		int flag = Integer.parseInt(request.getParameter("flag"));
		String nav = request.getParameter("nav");

		List<Software> commendList = softwareDao.getAllTopsByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, flag); // 编辑推荐，显示10个

		String title = null;
		String location = null;
		String name = null;

		// 分类
		List<Type> list = (List<Type>) otherCache.get("soft_list_type_list");
		if (list == null) {
			list = typeDao.getAllTypeByPage(1000, 1, flag); // 获取应用分类
			otherCache.put("soft_list_type_list", list);
		}

		if (id == 0) {
			// 所有信息
			name = "应用分类";
			title = "安卓应用";
			location = "安卓应用";
			softList = softwareDao.getAllSortByPageForWeb(
					ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort,
					ProductConfig.FLAG_SOFT);
			Integer countrecord = softwareDao.getAllSortByPageForWeb(sort,
					ProductConfig.FLAG_SOFT);

			Page page = new Page(countrecord, pageNo);
			request.setAttribute("page", page);

		} else {
			// 分类
			Type type = (Type) typeDao.getById(Type.class, id);
			if (type != null) {
				title = type.getName();
				location = type.getName();
				name = "游戏分类";
				softList = softwareDao.getAllTypeInfoByPageAndSort(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, flag, id,
						sort);
				Integer countrecord = softwareDao
						.getCountTypeInfoByPageAndSort(flag, id, sort);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			} else {
				name = "游戏分类";
				title = "安卓游戏";
				location = "安卓游戏";
				softList = softwareDao.getAllSortByPageForWeb(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort,
						ProductConfig.FLAG_GAME);
				Integer countrecord = softwareDao.getAllSortByPageForWeb(sort,
						ProductConfig.FLAG_GAME);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			}
		}
		request.setAttribute("list", list);
		request.setAttribute("title", title);// 网页标题
		request.setAttribute("location", location);// 当前位置
		request.setAttribute("nav", nav); // 导航栏
		request.setAttribute("name", name); // 分类/专题名称
		request.setAttribute("flag", flag); // 应用OR游戏OR专题
		request.setAttribute("id", id);// 分类OR专题ID
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("softList", softList); // 软件列表
		request.setAttribute("sort", sort); // 排序方式

		return mapping.findForward("list");
	}

	@SuppressWarnings("unchecked")
	public ActionForward resList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List<Software> softList = null;

		int pageNo = 1;

		String p = request.getParameter("n");
		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		int sort = Integer.parseInt(request.getParameter("s"));

		int id = Integer.parseInt(request.getParameter("i"));
		int flag = Integer.parseInt(request.getParameter("flag"));
		String nav = request.getParameter("nav");

		List<Software> commendList = softwareDao.getAllTopsByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, flag); // 编辑推荐，显示10个

		String title = null;
		String location = null;
		String name = null;
		List<Resources> list = (List<Resources>) otherCache.get("res_list_type_list");
		if (list == null) {
			list = resourcesDao.getAllTypeByPage(1000, 1); // 获取专题分类
			otherCache.put("res_list_type_list", list);
		}

		// 专题
		name = "专题分类";
		if (id == 0) {
			title = "安卓专题";
			location = "安卓专题";
			softList = softwareDao.getAllResInfoByPage(
					ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort);
			Integer countrecord = softwareDao.getCountResInfoByPage(sort);

			Page page = new Page(countrecord, pageNo);
			request.setAttribute("page", page);
		} else {
			Resources res = (Resources) resourcesDao.getById(Resources.class,
					id);
			if (res != null) {
				title = res.getName();
				location = res.getName();
				softList = softwareDao.getAllResInfoByPageAndSort(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, id, sort);
				Integer countrecord = softwareDao.getCountResInfoByPageAndSort(
						id, sort);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			} else {
				title = "安卓专题";
				location = "安卓专题";
				softList = softwareDao.getAllResInfoByPage(
						ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, sort);
				Integer countrecord = softwareDao.getCountResInfoByPage(sort);

				Page page = new Page(countrecord, pageNo);
				request.setAttribute("page", page);
			}
		}
		request.setAttribute("list", list); // 分类OR专题列表
		request.setAttribute("title", title);// 网页标题
		request.setAttribute("location", location);// 当前位置
		request.setAttribute("nav", nav); // 导航栏
		request.setAttribute("name", name); // 分类/专题名称
		request.setAttribute("flag", flag); // 应用OR游戏OR专题
		request.setAttribute("id", id);// 分类OR专题ID
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("softList", softList); // 软件列表
		request.setAttribute("sort", sort); // 排序方式

		return mapping.findForward("list");
	}

	// 软件详细信息
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		int id = Integer.parseInt(request.getParameter("i"));
		int flag = Integer.parseInt(request.getParameter("flag"));
		int sort = Integer.parseInt(request.getParameter("s"));
		String nav = request.getParameter("nav");

		Software software = (Software) softwareDao.getById(Software.class, id);
		List<Photo> photoList = photoDao.getAllBySoftId(id);

		Type type = (Type) typeDao.getById(Type.class, software.getType()
				.getId());
		List<Software> commendList = softwareDao.getAllTopsByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, flag); // 编辑推荐，显示10个
		int typeId = software.getType().getId();
		List<Software> connectList = softwareDao.getAllTopByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, typeId);// 相关软件，显示10个

		request.setAttribute("title", software.getName());// 网页标题
		request.setAttribute("type", type);// 当前位置
		request.setAttribute("nav", nav); // 导航栏
		request.setAttribute("flag", flag);
		request.setAttribute("sort", sort);
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("connectList", connectList);// 相关软件
		request.setAttribute("photoList", photoList);
		request.setAttribute("software", software);

		return mapping.findForward("detail");
	}

	// 软件详细信息
	public ActionForward info(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		int id = Integer.parseInt(request.getParameter("i"));

		Software software = (Software) softwareDao.getById(Software.class, id);
		List<Photo> photoList = photoDao.getAllBySoftId(id);
		Type type = (Type) typeDao.getById(Type.class, software.getType()
				.getId());
		List<Software> commendList = softwareDao.getAllTopsByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1); // 编辑推荐，显示10个

		List<Software> connectList = softwareDao.getAllTopByPage(
				ProductConfig.PAGE_SIZE_FOR_COMMONT, 1, software.getType()
						.getId());

		request.setAttribute("title", software.getName());// 网页标题
		request.setAttribute("type", type);// 当前位置
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("connectList", connectList);// 相关软件
		request.setAttribute("photoList", photoList);
		request.setAttribute("software", software);

		return mapping.findForward("info");
	}

	@SuppressWarnings("unchecked")
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		request.setCharacterEncoding(ProductConfig.CHARSET);
		String keyword = request.getParameter("k");
		String p = request.getParameter("n");
		int flag = Integer.parseInt(request.getParameter("f"));
		if (keyword.length() != 0) {
			// 增加搜索记录
			Search search = new Search();
			String tel = ProductUtils.getIpAddr(request);
			search.setTel(tel);
			search.setSearch(keyword);
			search.setTime(new Timestamp(System.currentTimeMillis()));
			search.setStatus(0);
			softwareDao.save(search);
		}

		int countrecord = softwareDao.getCountForSearch(keyword, flag);
		int pageNo = 1;

		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		Page page = new Page(countrecord, pageNo);

		List<Software> softList = softwareDao.getAllInfoForSearch(
				ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, keyword, flag);

		List<Software> commendList = (List<Software>) otherCache
				.get("search_commendList");
		if (commendList == null) {
			commendList = softwareDao.getAllTopsByPage(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1); // 编辑推荐，显示10个
			otherCache.put("search_commendList", commendList);
		}

		List<Software> connectList = (List<Software>) otherCache
				.get("search_connectList");
		if (connectList == null) {
			connectList = softwareDao.getAllSearchCommend(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1);// 相关软件，显示10个
			otherCache.put("search_connectList", connectList);
		}

		request.setAttribute("title", "搜索 - " + keyword);// 网页标题
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("connectList", connectList);// 相关软件
		request.setAttribute("page", page);
		request.setAttribute("softList", softList); // 软件列表
		request.setAttribute("keyword", URLEncoder.encode(keyword, "UTF-8"));
		request.setAttribute("key", keyword);
		request.setAttribute("flag", flag);

		return mapping.findForward("search");
	}

	@SuppressWarnings("unchecked")
	public ActionForward s(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		String keyword = request.getParameter("k");
		String p = request.getParameter("n");
		int flag = Integer.parseInt(request.getParameter("f"));
		String key = null;
		if (keyword.length() != 0) {
			key = new String(keyword.getBytes("ISO8859-1"), "UTF-8");
		} else {
			key = "";
		}
		if (keyword.length() != 0) {

			// 增加搜索记录
			Search search = new Search();
			String tel = ProductUtils.getIpAddr(request);
			search.setTel(tel);
			search.setSearch(keyword);
			search.setTime(new Timestamp(System.currentTimeMillis()));
			search.setStatus(0);
			softwareDao.save(search);
		}

		int countrecord = softwareDao.getCountForSearch(key, flag);
		int pageNo = 1;

		if (p != null) {
			pageNo = Integer.parseInt(p);
		}
		Page page = new Page(countrecord, pageNo);

		List<Software> softList = softwareDao.getAllInfoForSearch(
				ProductConfig.PAGE_SIZE_FOR_LIST, pageNo, key, flag);

		List<Software> commendList = (List<Software>) otherCache
				.get("search_commendList");
		if (commendList == null) {
			commendList = softwareDao.getAllTopsByPage(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1); // 编辑推荐，显示10个
			otherCache.put("search_commendList", commendList);
		}

		List<Software> connectList = (List<Software>) otherCache
				.get("search_connectList");
		if (connectList == null) {
			connectList = softwareDao.getAllSearchCommend(
					ProductConfig.PAGE_SIZE_FOR_COMMONT, 1);// 相关软件，显示10个
			otherCache.put("search_connectList", connectList);
		}

		request.setAttribute("title", "搜索 - " + keyword);// 网页标题
		request.setAttribute("commendList", commendList);// 推荐软件
		request.setAttribute("connectList", connectList);// 相关软件
		request.setAttribute("page", page);
		request.setAttribute("softList", softList); // 软件列表
		request.setAttribute("keyword", URLEncoder.encode(key, "UTF-8"));
		request.setAttribute("key", key);
		request.setAttribute("flag", flag);

		return mapping.findForward("search");
	}
}
